package com.example.cdctravel

import android.os.Bundle
import android.view.View
import android.widget.EditText
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity

class AddActivity : AppCompatActivity(), View.OnClickListener {
    var note_back: ImageView? = null
    var content: EditText? = null
    var editTitle: EditText? = null
    var delete: ImageView? = null
    var note_save: ImageView? = null
    var mSQLiteHelper: SQLiteHelper? = null
    var noteName: TextView? = null
    var id: String? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_add)

        //声明控件
        note_back = findViewById<View>(R.id.note_back) as ImageView
        editTitle = findViewById<View>(R.id.note_title) as EditText
        content = findViewById<View>(R.id.note_content) as EditText
        delete = findViewById<View>(R.id.delete) as ImageView
        note_save = findViewById<View>(R.id.note_save) as ImageView
        noteName = findViewById<View>(R.id.note_name) as TextView
        //设置控件监听事件
        note_back!!.setOnClickListener(this)
        delete!!.setOnClickListener(this)
        note_save!!.setOnClickListener(this)
        //初始化数据
        initData()
    }

    protected fun initData() {
        mSQLiteHelper = SQLiteHelper(this)
        noteName!!.text = "添加"
        //NotepadActivity  获取上个页面传递过来数据
        val intent = intent
        if (intent != null) {
            id = intent.getStringExtra("id")
            if (id != null) {
                noteName!!.text = "修改"
                content!!.setText(intent.getStringExtra("content"))
                editTitle!!.setText(intent.getStringExtra("time"))
            }
        }
    }

    //点击事件监听
    override fun onClick(v: View) {
        when (v.id) {
            R.id.note_back -> finish() //关闭
            R.id.delete -> {
                content!!.setText("") //清空
                editTitle!!.setText("") //清空
                if (id != null) {
                    mSQLiteHelper!!.deleteData(id!!)
                    showToast("删除成功!")
                    setResult(2)
                }
            }

            R.id.note_save -> {
                //获取输入内容
                val mTitle = editTitle!!.text.toString().trim { it <= ' ' }
                val noteContent = content!!.text.toString().trim { it <= ' ' }
                if (id != null) { //修改操作
                    if (noteContent.length > 0) {
                        //更新数据
                        if (mSQLiteHelper!!.updateData(id!!, noteContent, mTitle)) {
                            showToast("修改成功")
                            //回调给上个页面  刷新数据
                            setResult(2)
                            finish()
                        } else {
                            showToast("修改失败")
                        }
                    } else {
                        showToast("修改内容不能为空!")
                    }
                } else {
                    //向数据库中添加数据
                    if (noteContent.length > 0) {
                        if (mSQLiteHelper!!.insertData(noteContent, mTitle)) {
                            showToast("保存成功")
                            setResult(2)
                            finish()
                        } else {
                            showToast("保存失败")
                        }
                    } else {
                        showToast("修改内容不能为空!")
                    }
                }
            }
        }
    }

    fun showToast(message: String?) {
        Toast.makeText(this@AddActivity, message, Toast.LENGTH_SHORT).show()
    }
}